package test;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import app.AppWorkSession;
import app.datavalue.DeliveryRepMsgInfo;
import app.datavalue.HandlingInfo;
import app.datavalue.MessageStatus;
import app.datavalue.MsgInfo;
import app.model.IMessageStorage;
import app.model.StorageResult;
public class MockMessageStorage implements IMessageStorage 
{
	
	//ArrayList<MsgInfo> incommingMsg;

	
	/**
	 * @directed true
	 */
	
	private AppWorkSession lnkAppWorkSession;

	static int ID_COUNTER;
	
	Map<Integer, MsgInfo> incommingMsgs;
	
	Map<Integer, MsgInfo> outcommingMsgs;
	
	Map<Integer, DeliveryRepMsgInfo> deliveryReps;
	
	public MockMessageStorage()
	{
		incommingMsgs = new LinkedHashMap<Integer, MsgInfo>();
		outcommingMsgs = new LinkedHashMap<Integer, MsgInfo>();
		
		deliveryReps = new LinkedHashMap<Integer, DeliveryRepMsgInfo>();
	}
	
	/* (non-Javadoc)
	 * @see test.IMessageStorage#findHandlingInfo(java.lang.String)
	 */
	public HandlingInfo findHandlingInfo( String messageBody ) 
	{
		//Commented automatically
		//return;
		
		if ( messageBody.indexOf("file2get") == -1 )
			return null;
		
		HandlingInfo hi = new HandlingInfo();
		hi.setResponse( "http://ya.ru" );
		return hi;
	}

	
	public int genUnId()
	{
		return ID_COUNTER++;
	}
	
	
	/* (non-Javadoc)
	 * @see test.IMessageStorage#updateInMsg(app.datavalue.MsgInfo)
	 */
	public void updateInMsg( MsgInfo msgInfo )
	{
		if ( msgInfo.getInternalId() == null )
		{
			msgInfo.setInternalId( genUnId() );
		}

		incommingMsgs.put( msgInfo.getInternalId() , msgInfo );

		return;		 
	}
	
	/* (non-Javadoc)
	 * @see test.IMessageStorage#updateOutMsg(app.datavalue.MsgInfo)
	 */
	public void updateOutMsg( MsgInfo msgInfo )
	{
		if ( msgInfo.getInternalId() == null )
		{
			msgInfo.setInternalId( genUnId() );
		}

		outcommingMsgs.put( msgInfo.getInternalId() , msgInfo );

		return;			
	}

	/* (non-Javadoc)
	 * @see test.IMessageStorage#findInMsgInfoById(java.lang.Integer)
	 */
	public MsgInfo findInMsgInfoById(Integer internalId) 
	{
		MsgInfo msgInfo = incommingMsgs.get( internalId );
		
		return msgInfo;
	}

	/* (non-Javadoc)
	 * @see test.IMessageStorage#findOutMsgInfoById(java.lang.Integer)
	 */
	public MsgInfo findOutMsgInfoById(Integer internalId) 
	{
		return outcommingMsgs.get( internalId );
	}

	
	
	/* (non-Javadoc)
	 * @see test.IMessageStorage#getUnProcOutMsgsInNonCurrWorkSession()
	 */
	public StorageResult<MsgInfo> getUnProcOutMsgsInNonCurrWorkSession() 
	{
		return getMsgsByState( outcommingMsgs , new MsgInfoUnProcMsgsInNonCurrWorkSession() );
	}

	/* (non-Javadoc)
	 * @see test.IMessageStorage#getUnProcIncMsgsNonCurrWorkSession()
	 */
	public StorageResult<MsgInfo> getUnProcIncMsgsNonCurrWorkSession() 
	{
		return getMsgsByState( incommingMsgs , new MsgInfoUnProcMsgsInNonCurrWorkSession() );
	}	
	
	public static class MsgInfoUnProcMsgsInNonCurrWorkSession implements IMsgInfoAccepter
	{

		public boolean accept(MsgInfo msgInfo) 
		{
			
			return msgInfo.getStatus() == MessageStatus.IN_PROCESSING 
					&& 
				   msgInfo.getWorkSessionId().compareTo( AppWorkSession.getInstace().getCurrentSessionId() ) != 0;

		}
		   
	}
	
	public interface IMsgInfoAccepter
	{
		boolean accept( MsgInfo msgInfo );
	}
	
	protected StorageResult<MsgInfo> getMsgsByState( Map<Integer, MsgInfo> map , IMsgInfoAccepter accepter )
	{
		ArrayList<MsgInfo> arrList = new ArrayList<MsgInfo>();
		
		for ( MsgInfo msgInfo : map.values() )
		{
			/*
			if ( msgInf.status == msgStatus )
				arrList.add( msgInf );
			*/
			
			if ( accepter.accept(msgInfo) )
				arrList.add( msgInfo );
			
		}
		
		return new StorageResult<MsgInfo>( null , arrList );
		
	}

	/* (non-Javadoc)
	 * @see test.IMessageStorage#updateDeliveryReportInfo(app.datavalue.DeliveryRepMsgInfo)
	 */
	public void updateDeliveryReportInfo(DeliveryRepMsgInfo deliveryMsgInfo) 
	{
		if ( deliveryMsgInfo.getInternalId() == null )
			deliveryMsgInfo.setInternalId( genUnId() );
		
		deliveryReps.put( deliveryMsgInfo.getInternalId() , deliveryMsgInfo );
	}
	
	/* (non-Javadoc)
	 * @see test.IMessageStorage#findDeliveryRepMsgInfoByExternalId(java.lang.String)
	 */
	public DeliveryRepMsgInfo findDeliveryRepMsgInfoByExternalId( String externalId )
	{
		//return deliveryReps.get( id );
		
		for ( DeliveryRepMsgInfo repInfo : deliveryReps.values() )
		{
			if ( repInfo.getExternalId() == externalId )
				return repInfo;
		}
		
		return null;
		
	}
	



	
	
	
}
